package com.lut.edu.apm.webCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lut.edu.apm.webCenter.dao.UserInfoMapper;
import com.lut.edu.apm.webCenter.pojo.UserInfo;
import com.lut.edu.apm.webCenter.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Zhao Suzhen
 * @date `2024/4/17`
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public UserInfo queryInfoByPhone(String phoneNumber) {
        UserInfo userInfo = userInfoMapper.selectUserByPhone(phoneNumber);
        return userInfo;
    }

    @Override
    public UserInfo queryInfoByUserId(String userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return userInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean isUserLogin(String phoneNumber) {
        return null != userInfoMapper.selectUserByPhone(phoneNumber);
    }

    @Override
    public void updateUserPoints(String phoneNumber, Long pointsToAdd) {
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserInfo::getPhoneNumber, phoneNumber).setSql("user_points = user_points + " + pointsToAdd);
        userInfoMapper.update(null, updateWrapper);
    }

    @Override
    public void updateUserSex(String userId, String sex) {
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserInfo::getUserId, userId).setSql("sex = " + sex);
        userInfoMapper.update(null, updateWrapper);
    }


}
